﻿using System.Collections.Generic;
using System.Linq;
using DynamicChart.Service.FunctionConversion;
using DynamicChart.Util;

namespace DynamicChart.Service.Step.GenerateSQL;

public class GenerateSelectStep : GenerateAbstractSQLStep
{
    public GenerateSelectStep(ISqlFunctionAdapter adapter) : base(adapter)
    {
    }

    public override void Process(IDictionary<string, object> data, IDictionary<string, string> res)
    {
        var selectFields = DataProcessor.EnumerateFields(data)
            .SelectMany(t =>
            {
                var display = CreateField(t.Entity, $"{t.TableKey}.{t.ColumnKey}", "DisplayMethods");
                var polymer = CreateField(t.Entity, $"{t.TableKey}.{t.ColumnKey}", "PolymerizationMode");

                var result = new List<string>();
                if (!string.IsNullOrEmpty(display)) result.Add(display);
                if (!string.IsNullOrEmpty(polymer) 
                    && t.Entity.TryGetValue("IsXaxis", out var isXaxis) &&
                    isXaxis.ToString().ToLower() == "true"
                    ) 
                    result.Add(polymer + " as XAXIS");
                return result;
            })
            .ToList();

        res["select"] = string.Join(", ", selectFields);
    }
}